<?php


$RSS_Content = array();

function RSS_Tags($item, $type)
{
		$y = array();
		
      	
            $tnl = $item->getElementsByTagName("title");
      	$tnl = $tnl->item(0);
      	$title = $tnl->firstChild->data;
      	

		$tnl = $item->getElementsByTagName("link");
		$tnl = $tnl->item(0);
		$link = $tnl->firstChild->data;

            

		$tnl = $item->getElementsByTagName("description");
		$tnl = $tnl->item(0);
		$description = $tnl->firstChild->data;
		
		
		$tnl = $item->getElementsByTagName("author");
		$tnl = $tnl->item(0);
		$author = $tnl->firstChild->data;
		
		$tnl = $item->getElementsByTagName("pubDate");
	      $tnl = $tnl->item(0);
	      $pubDate = $tnl->firstChild->data;

            if ($type != 0)
            {
		    $y["title"] = $title;
		    $y["link"] = $link;
		    $y["description"] = $description;
		    $y["type"] = $type;
		    $y["author"] = $author;
                $y["pubDate"] = $pubDate;
               
		}
		else
		{
		  $y["title"] = ""; //$title;
		  $y["link"] = ""; //"informio_digest.html";
		  //$result = strchr($title, "-");
      $result = $title;
      $result2 = urlencode( iconv('utf-8', 'windows-1251', $link ) );
		  $y["description"] = "<nobr>" . $result . 
        "&nbsp;<a href=rss_feed.php?query_meta=".$result2."&query_mx=".$result2."&fulltext=1>".
        "<img height=13 width=13 border=0 src=rss-feed-icon-small.gif style='position: static; margin-bottom: -2px;'/></a></nobr>";
	    $y["type"] = $type;
      $y["author"] = "";
      $y["pubDate"] = "";  
		}
		
		return $y;
}


function RSS_Channel($channel)
{
	global $RSS_Content;

	$items = $channel->getElementsByTagName("item");
	
	// Processing channel
	
	$y = RSS_Tags($channel, 0);		// get description of channel, type 0
	array_push($RSS_Content, $y);
	
	// Processing articles
	
	foreach($items as $item)
	{
		$y = RSS_Tags($item, 1);	// get description of article, type 1
		array_push($RSS_Content, $y);
	}
}

function RSS_Retrieve($url)
{
	global $RSS_Content;

	$doc  = new DOMDocument();
	$doc->load($url);

	$channels = $doc->getElementsByTagName("channel");
	
	$RSS_Content = array();
	
	foreach($channels as $channel)
	{
		 RSS_Channel($channel);
	}
	
}


function RSS_RetrieveLinks($url)
{
	global $RSS_Content;

	$doc  = new DOMDocument();
	$doc->load($url);

	$channels = $doc->getElementsByTagName("channel");
	
	$RSS_Content = array();
	
	foreach($channels as $channel)
	{
		$items = $channel->getElementsByTagName("item");
		foreach($items as $item)
		{
			$y = RSS_Tags($item, 1);	// get description of article, type 1
			array_push($RSS_Content, $y);
		}
		 
	}

}


function RSS_Links($url, $size)
{
	global $RSS_Content;

	$page = "<ul>";

	RSS_RetrieveLinks($url);
	if($size > 0)
		$recents = array_slice($RSS_Content, 0, $size);

	foreach($recents as $article)
	{
		$type = $article["type"];
		if($type == 0) continue;
		$title = $article["title"];
		$link = $article["link"];
		$page .= "<li><a href=\"$link\">$title</a></li>\n";			
	}

	$page .="</ul>\n";

	return $page;
	
}



function RSS_Display($url, $size)
{
	global $RSS_Content;

	$opened = false;
	$page = "";

	RSS_Retrieve($url);
	if($size > 0)
		$recents = array_slice($RSS_Content, 0, $size);

	foreach($recents as $article)
	{
		$type = $article["type"];
		if($type == 0)
		{
			if($opened == true)
			{
				$page .="</table>";  
				$opened = false;
			}
			//$page .="<b>";
		}
		else
		{
			if($opened == false) 
			{
				$page .= "<table style='padding: 0px 10px 10px;'>"; 
				$opened = true;
			}
		}
		$title = $article["title"];
		$link = $article["link"];
		$description = $article["description"];
	 
            $author = $article["author"];
            $pubDate = $article["pubDate"];
		    
		$page .= "<tr><td style='padding: 15px 0px 0px 0px;'>";
  $page .= "<a href=\"$link\">$title</a>";  
            $page .= "</td></tr>";
            
            $page .= "<tr><td>";
            $page .= "<b>". $pubDate . "</b>";
            $page .= "</td></tr>";
            
            
		if($description != false)
		{             
		      $page .= "<tr><td>";
			$page .= $description; 
			$page .= "</td></tr>";
		}    
		if($type!=0)
            {	
      		$page .= "<tr><td>";  
      	      $page .= "<b>Источник: " . $author . "</b><br/>";
      		$page .= "</td></tr>";
      	}	
		
		if($type==0)
		{
			$page .="</b><br />";
		}

	}

	if($opened == true)
	{	
		$page .="</table>";
	}
	return $page."\n";
	
}


?>
